package gae.testjig.client;

import gae.testjig.client.ChannelProvider.ChannelReady;
import gae.testjig.client.ChannelProvider.ClientChannel;
import gae.testjig.client.UIFeedbackLabel.FeedbackListener;
import gae.testjig.client.eventbus.EchoEvent;
import gae.testjig.client.eventbus.ChannelMetaEvent;
import gae.testjig.client.eventbus.InfoEvent;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiConstructor;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;
import com.google.web.bindery.event.shared.SimpleEventBus;

public class UIJdoTest extends Composite {

	interface UIJdoTestUiBinder extends UiBinder<Widget, UIJdoTest> {}
	private static UIJdoTestUiBinder uiBinder = GWT.create(UIJdoTestUiBinder.class);

	@UiField Button btnEchoChannel;
	@UiField Button btnTest1;
	@UiField Button btnTest2;
	@UiField Button btnClearLog;
	@UiField UIFeedbackLabel lblFeedback;
	@UiField HTML txtConsoleLog;
	
	@UiConstructor
	public UIJdoTest(final GaeTestJigServiceAsync SERVICE, final ClientChannel cp, final SimpleEventBus eventBus) {
		initWidget(uiBinder.createAndBindUi(this));

		ChannelMetaEvent.register(eventBus, new ChannelMetaEvent.Handler() {
			@Override
			public void onFeedback(String message) {
				lblFeedback.setTextNoSpin(message);
			}
		});
		EchoEvent.registerToSource(eventBus, cp.getClientChannelId(), new EchoEvent.Handler() {
			@Override
			public void onEcho(EchoEvent event) {
				lblFeedback.setTextNoSpin(event.getMessage());
			}
		});
		InfoEvent.registerToSource(eventBus, cp.getClientChannelId(), new InfoEvent.Handler() {
			@Override
			public void onInfo(InfoEvent event) {
				lblFeedback.setTextNoSpin(event.getMessage());
			}
		});
		
		lblFeedback.addFeedbackListener(new FeedbackListener() {
			@Override
			public void onFeedback(String message) {
				append(message);
			}
		});

		btnTest1.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				cp.ensureOpenChannel(new ChannelReady() {
					@Override
					public void onChannelReady() {
						SERVICE.jdoTest1(cp.getClientChannelId(), new AsyncCallback<Void>() {
							@Override
							public void onSuccess(Void result) {
							}
							@Override
							public void onFailure(Throwable caught) {
								lblFeedback.setTextNoSpin(caught.getMessage());
							}
						});
					}
				});
			}
		});
		
		btnTest2.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				cp.ensureOpenChannel(new ChannelReady() {
					@Override
					public void onChannelReady() {
						SERVICE.jdoTest2(cp.getClientChannelId(), new AsyncCallback<Void>() {
							@Override
							public void onSuccess(Void result) {
							}
							@Override
							public void onFailure(Throwable caught) {
								lblFeedback.setTextNoSpin(caught.getMessage());
							}
						});
					}
				});
			}
		});

		btnEchoChannel.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				cp.ensureOpenChannel(new ChannelReady() {
					@Override
					public void onChannelReady() {
						SERVICE.sendEchoChannel(cp.getClientChannelId(), new AsyncCallback<Void>() {
							@Override
							public void onSuccess(Void result) {
							}
							@Override
							public void onFailure(Throwable caught) {
								lblFeedback.setTextNoSpin(caught.getMessage());
							}
						});
					}
				});
			}
		});

		btnClearLog.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				clearLog();
			}
		});
	}
	
	public void append(List<String> list) {
		for ( String msg : list ) {
			append(msg);
		}
	}	
	
	public void append(String msg) {
		String content = txtConsoleLog.getHTML();
		txtConsoleLog.setHTML(content + msg + "<br/>");
		txtConsoleLog.getElement().setScrollTop(txtConsoleLog.getElement().getScrollHeight()); // keep scrolled to bottom
	}
	
	public void clearLog() {
		txtConsoleLog.setHTML("");
	}

}
